<?php
/**
 * Created by PhpStorm.
 * User: 44766
 * Date: 2018-12-28
 * Time: 15:02
 */

namespace App\Services\RecommendService;


use App\Models\Favorite;
use App\Models\History;
use App\Models\Member;
use App\Models\Position;
use App\Models\Tagged;

class RecommendService
{
    public $member;

    public function __construct($member)
    {
        $this->member=$member;
    }

    public function getTags(){
        /** @var Member $member */
        $member         =$this->member;
        $filter_ids     = [];
        $favorite_tags  = collect();
        $favorite_posts = Position::with('tags')->whereHas('favorites', function ($member) {
            /** @var Favorite $member */
            $member->where('member_id', $member->id);
        })->get();
        foreach ($favorite_posts as $post) {
            $favorite_tags = $favorite_tags->merge($post->tags);
            $filter_ids[]  = $post->id;
        }


        $history_tags  = collect();
        $history_posts = Position::with('tags')->whereHas('histories', function ($member) {
            /** @var History $member */
            $member->where('member_id', $member->id);
        })->get();
        foreach ($history_posts as $post) {
            $history_tags = $history_tags->merge($post->tags);
        }

        $tags = [];
        foreach ($member->tags as $tagged) {
            if (!isset($tags[$tagged->tag_id])) {
                $tags[$tagged->tag_id] = 0;
            }
            $tags[$tagged->tag_id] += 30;
        }

        /** @var Tagged $tagged */
        foreach ($favorite_tags as $tagged) {
            if (!isset($tags[$tagged->tag_id])) {
                $tags[$tagged->tag_id] = 0;
            }
            $tags[$tagged->tag_id] += 10;
        }
        foreach ($history_tags as $tagged) {
            if (!isset($tags[$tagged->tag_id])) {
                $tags[$tagged->tag_id] = 0;
            }
            $tags[$tagged->tag_id] += 2;
        }
        $tags    = array_sort($tags);
        $tag_ids = (array_keys($tags));
        if (!$tag_ids) {
            return [];
        }
        $positions = Position::with(['tags.tag'])->whereHas('tags', function ($tag) use ($tag_ids) {
            /** @var Tagged $tag */
            $tag->whereIn('tag_id', $tag_ids);
        })->whereNotIn('positions.id', $filter_ids);

        $this->renderList($positions);

        $positions = $positions->get();


        foreach ($positions as $position) {
            $score     = 0;
            $tag_count = 0;
            foreach ($position->tags as $tagged) {
                if (isset($tags[$tagged->tag_id])) {
                    $score += $tags[$tagged->tag_id];
                    $tag_count++;
                }
            }
//            $score=round($score/$tag_count,2);
            $recommendation = round($score / 100, 2);
            $position->setAttribute('score', $score);
            $position->setAttribute('recommendation', $recommendation > 1 ? 1 : $recommendation);
        }
        $positions = $positions->sortByDesc('score')->values();
    }
}